<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of UserController
 *
 * @author Kamdem Kenmogne
 */
class UserController extends Controller {
    //put your code here
    
    protected $app;
    
    public function __construct($app) 
    {
        parent::__construct($app);
        $this->app = $app;
    }
    
    /**
     * get All User Registration
     * url - /register
     * method - GET
     */
    public function getUsers() 
    {
        $response = array();
        // call user
        $result = $this->app->myDB->getUsers();

        if($result != NULL){
            $this->app->authen->echoResponse(201, $result);
        }
        else{
            $response["error"] = TRUE;
            $response["message"] = "The requested resource doesn't exists";
            $this->app->authen->echoResponse(401, $response);
        }
    }
    
    /**
     * User Registration
     * url - /register
     * method - POST
     * params - name, email, password
     */
    public function addUser()
    {
        $this->app->authen->verifyRequiredParams(array('username','email','password','birthdate'));
        $response = array();
        // reading post params
        $username = $this->app->request()->post('username');
        $email = $this->app->request()->post('email');
        $password = $this->app->request()->post('password');
        $birthdate = $this->app->request()->post('birthdate');
        // validating email address
        $this->app->authen->validateEmail($email);

        // store user
        $res = $this->app->myDB->createUser($username,$email, $password, $birthdate);

        if ($res == USER_CREATED_SUCCESSFULLY){
            $response["error"] = false;
            $response["message"] = "You are successfully registered";
        }
        else if ($res == USER_CREATE_FAILED){
            $response["error"] = true;
            $response["message"] = "Oops! An error occurred while registereing";
        } 
        else if ($res == USER_ALREADY_EXISTED){
            $response["error"] = true;
            $response["message"] = "Sorry, this email already existed";
        }
        else if ($res == USERNAME_ALREADY_EXISTED){
            $response["error"] = true;
            $response["message"] = "Sorry, this username already existed";
        }
        // echo json response
        $this->app->authen->echoResponse(201, $response);
    }
    
    public function loginUser()
    {
        // reading post params
        $email = $this->app->request()->post('email');
        $password = $this->app->request()->post('password');
        //
        $response = array();
        // check for correct email and password
        if ($this->app->myDB->checkLogin($email, $password)) {
            // get the user by email
            $user = $this->app->myDB->getUserByEmail($email);

            if ($user != NULL) {
                $response["error"] = false;
                $response['username'] = $user['username'];
                $response['email'] = $user['email'];
                $response['apiKey'] = $user['api_key'];
                $response['message'] = "LOGIN SUCCESS";
                //$response['created_at'] = $user['created_at'];
            } else {
                // unknown error occurred
                $response['error'] = true;
                $response['message'] = "An error occurred. Please try again";
            }
        } else {
            // user credentials are wrong
            $response['error'] = true;
            $response['message'] = 'Login failed. Incorrect credentials';
        }
        $this->app->authen->echoResponse(200, $response); 
    }
    
    
    public function getUser($email)
    {
        $response = array();
        // call user
        $result = $this->app->myDB->getUserByEmail($email);

        if($result != NULL){
            $this->app->authen->echoResponse(200, $result);
        }
        else{
            $response["error"] = TRUE;
            $response["message"] = "The requested resource doesn't exists";
            $this->app->authen->echoResponse(401, $response);
        }
    }
}
